import org.junit.Assert;

import javax.naming.NamingEnumeration;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.WeakHashMap;

public class NGuessNumber {
    private NGuessNumberTest nGuessNumberTest = new NGuessNumberTest();
    private static Scanner sc = new Scanner(System.in);
    public int[] generateAnswer() {
        Random random = new Random();
        int[] nums = new int[4];
        int index = 0;
        while (index < 4) {
            int num = random.nextInt(10);
//            System.out.println(Arrays.toString(nums));
            if (!String.valueOf(Arrays.toString(nums)).contains(String.valueOf(num))) {
                nums[index] = num;
                index++;
            }


        }
//        System.out.println(Arrays.toString(nums));
        // nGuessNumberTest.generateAnswerTest(nums);
        return nums;
    }


    public int[] getPlayerInput() {
        int[] nums = new int[4];

        for (int i = 0; i < 4; i++) {
            System.out.println(String.format("输入猜测数字第%s个:", i + 1));
            nums[i] = sc.nextInt();
        }
        // Assert.assertEquals("答案数量错误", nums.length, 4);
        return nums;
    }

    public String[] compareGuessAnswer(int[] paw, int[] inp) {
//        System.out.println(Arrays.toString(paw));
//        System.out.println(Arrays.toString(inp));
        String[] yn = new String[4];
        for (int i = 0; i < 4; i++) {
            if (paw[i] == inp[i]) {
                yn[i] = "A";
            } else {
                yn[i] = "N";
            }
        }
        return yn;

    }

    public boolean isWin(String[] str) {
        int num = 0;
        for (String i : str) {
            if (i == "A"){
                num++;
            }
        }
        if (num == 4){
            return true;
        }
        return false;

    }

    public static void main(String[] args) {

        NGuessNumber nGuessNumber = new NGuessNumber();

//        nGuessNumber.generateAnswer();
//        nGuessNumber.getPlayerInput();

        while (true){
            int[] password = nGuessNumber.generateAnswer();
//            System.out.println(Arrays.toString(password));

            for (int i = 0; i < 10; i++) {
                System.out.println(String.format("第%s次猜测",i+1));
                String[] A = nGuessNumber.compareGuessAnswer(password, nGuessNumber.getPlayerInput());
                if (nGuessNumber.isWin(A)){
                    System.out.println("游戏获胜");
                    break;
                }
                System.out.println("您当前猜中了(A为正确答案):");
                System.out.println(Arrays.toString(A));
                System.out.println("根据提示在试试吧!");

                if (i == 10){
                    System.out.println("10次机会用完啦!");
                }
            }
//            while (true){
////
////            }
            System.out.println("输入Y继续游戏/否则退出游戏:");
            String yn = sc.next();

            if(yn.equals("Y")){
                continue;
            }else {
                System.out.println("退出游戏");
                break;
            }


        }


    }


}
